package com.cms.fom.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cms.common.utils.R;
import com.cms.fom.dao.InfoDao;
import com.cms.fom.dao.ProcessDao;
import com.cms.fom.entity.InfoEntity;
import com.cms.fom.entity.ProcessEntity;
import com.cms.fom.entity.vo.ProcessVo;
import com.cms.fom.entity.vo.StuProcessVo;
import com.cms.fom.feign.StuFeign;
import com.cms.fom.to.StudentTo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.common.utils.PageUtils;
import com.cms.common.utils.Query;

import com.cms.fom.dao.StuProcessDao;
import com.cms.fom.entity.StuProcessEntity;
import com.cms.fom.service.StuProcessService;

import javax.annotation.Resource;


@Service("stuProcessService")
public class StuProcessServiceImpl extends ServiceImpl<StuProcessDao, StuProcessEntity> implements StuProcessService {

    @Resource
    private ProcessDao processDao;

    @Resource
    private InfoDao infoDao;

    @Autowired
    private StuFeign stuFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StuProcessEntity> page = this.page(
                new Query<StuProcessEntity>().getPage(params),
                new QueryWrapper<StuProcessEntity>()
        );

        List<StuProcessVo> result = page.getRecords().stream().map(item -> {
            StuProcessVo stuProcessVo = new StuProcessVo();
            Long studentId = item.getStudentId();
            R r = stuFeign.info(studentId);
            StudentTo student = r.getData("student", new TypeReference<StudentTo>() {
            });
            BeanUtils.copyProperties(item, stuProcessVo);
            stuProcessVo.setStudentName(student.getName());
            String[] idsString = item.getCurrent().split(",");
            List<Long> ids = Arrays.asList(idsString).stream().map(id -> Long.parseLong(id)).collect(Collectors.toList());
            String current = item.getCurrent();
            List<InfoEntity> infoEntities = infoDao.selectList(new QueryWrapper<InfoEntity>().in("id", ids));
            List<String> list = infoEntities.stream().map(infoEntity -> infoEntity.getTitle()).collect(Collectors.toList());
            stuProcessVo.setProcessContent(StringUtils.join(list, ","));
            return stuProcessVo;
        }).collect(Collectors.toList());

        IPage<StuProcessVo> resultPage = new Page<>();
        resultPage.setTotal(page.getTotal());
        resultPage.setSize(page.getSize());
        resultPage.setPages(page.getPages());
        resultPage.setCurrent(page.getCurrent());
        resultPage.setRecords(result);

        return new PageUtils(resultPage);
    }

    @Override
    public ProcessVo selectAllAndCurrent(Long id) {
        ProcessVo processVo = new ProcessVo();
        StuProcessEntity stuProcessEntity = baseMapper.selectOne(new QueryWrapper<StuProcessEntity>().eq("student_id", id).orderByDesc("update_time"));
        BeanUtils.copyProperties(stuProcessEntity, processVo);
        ProcessEntity processEntity = processDao.selectOne(new QueryWrapper<ProcessEntity>().orderByDesc("update_time"));
        String[] infoIds = processEntity.getContent().split(",");
        List<Long> ids = Arrays.stream(infoIds).map(item -> {
            Long infoId = Long.parseLong(item);
            return infoId;
        }).collect(Collectors.toList());
        List<InfoEntity> infoEntities = infoDao.selectBatchIds(ids);
        processVo.setProcess(infoEntities);

        return processVo;
    }

    @Override
    public ProcessVo selectStudentProcessByNumOrPhone(String num) {
        ProcessVo processVo = new ProcessVo();
        R r = stuFeign.infoByNum(num);
        if(r.getCode() == 0){
            StudentTo student = r.getData("student", new TypeReference<StudentTo>() {
            });
            processVo.setStudentName(student.getName());
            Long id = student.getId();
            StuProcessEntity stuProcessEntity = baseMapper.selectOne(new QueryWrapper<StuProcessEntity>().eq("student_id", id).orderByDesc("update_time"));
            BeanUtils.copyProperties(stuProcessEntity, processVo);
            ProcessEntity processEntity = processDao.selectOne(new QueryWrapper<ProcessEntity>().orderByDesc("update_time"));
            String[] infoIds = processEntity.getContent().split(",");
            List<Long> ids = Arrays.stream(infoIds).map(item -> {
                Long infoId = Long.parseLong(item);
                return infoId;
            }).collect(Collectors.toList());
            List<InfoEntity> infoEntities = infoDao.selectBatchIds(ids);
            processVo.setProcess(infoEntities);
            return processVo;
        }else{
            return null;
        }
    }

}